﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using MobileGIS.Carto;

namespace MobileGIS.UI
{
    public partial class MapControl : UserControl
    {
        private Bitmap m_bmpFlash;
        private int m_countFlash;
        private Image m_imgFlash;
        private int m_indexFlash;
        private bool m_isMapRefresh;

        private Map m_map;
        private IFeature m_selectFeature;
        protected internal Graphics m_grahics;
        protected internal IntPtr m_hDC = IntPtr.Zero;
        protected internal PictureBox m_picMap;
        private Timer m_timerFlash;



        public MapControl()
        {
            //InitializeComponent();
            this.InitComponent();

            this.m_map = null;
            this.m_isMapRefresh = false;

        }

        private void InitComponent()
        {
            this.m_picMap = new PictureBox();
            this.m_timerFlash = new Timer();
            base.SuspendLayout();
            this.m_picMap.BackColor = Color.Gainsboro;
            this.m_picMap.Dock = DockStyle.Fill;
            this.m_picMap.Location = new Point(0, 0);
            this.m_picMap.Name = "PicMap";
            this.m_picMap.Size = new Size(150, 150);

            this.m_picMap.DoubleClick += new EventHandler(picMap_DoubleClick);
            this.m_picMap.LostFocus += new EventHandler(picMap_LostFocus);
            this.m_picMap.Click += new EventHandler(picMap_Click);
            this.m_picMap.MouseDown += new MouseEventHandler(m_picMap_MouseDown);
            this.m_picMap.MouseMove += new MouseEventHandler(m_picMap_MouseMove);
            this.m_picMap.GotFocus += new EventHandler(m_picMap_GotFocus);
            this.m_picMap.Resize += new EventHandler(picMap_Resize);
            this.m_picMap.ParentChanged += new EventHandler(m_picMap_ParentChanged);
            this.m_picMap.MouseUp += new MouseEventHandler(m_picMap_MouseUp);
            this.m_picMap.EnabledChanged += new EventHandler(m_picMap_EnabledChanged);
            this.m_timerFlash.Tick += new EventHandler(m_timerFlash_Tick);

            base.AutoScaleDimensions = new SizeF(96f, 96f);
            base.AutoScaleMode = AutoScaleMode.Dpi;

            this.BackColor = Color.White;
            base.Controls.Add(this.m_picMap);
            base.Name = "MapControl";
            base.ResumeLayout(false);
        }

        void m_timerFlash_Tick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void m_picMap_EnabledChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void m_picMap_MouseUp(object sender, MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        void m_picMap_ParentChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void picMap_Resize(object sender, EventArgs e)
        {
            if (this.m_picMap.Image != null && (this.m_picMap.Image.Width != this.m_picMap.Width || this.m_picMap.Image.Height != this.m_picMap.Height))
            {
                this.m_picMap.Image = null;
                if (this.m_hDC != IntPtr.Zero)
                {
                    this.m_grahics.ReleaseHdc(this.m_hDC);
                    this.m_grahics.Dispose();
                    this.m_hDC = IntPtr.Zero;
                }
            }

            if (this.m_picMap.Image == null)
            {
                this.m_picMap.Image = Image.FromHbitmap(new Bitmap(base.Width,base.Height).GetHbitmap());
                this.m_grahics = Graphics.FromImage(this.m_picMap.Image);
                this.m_grahics.Clear(Color.White);
                this.m_hDC = this.m_grahics.GetHdc();
            }

            this.OnResize(e);
        }

        void m_picMap_GotFocus(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void m_picMap_MouseMove(object sender, MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        void m_picMap_MouseDown(object sender, MouseEventArgs e)
        {
            throw new NotImplementedException();
        }

        void picMap_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void picMap_LostFocus(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        

        void picMap_DoubleClick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }


        #region 绘制

        public void DrawEllipse(Pen pen, Rectangle rect)
        {
            this.m_grahics.DrawEllipse(pen, rect);
            this.m_picMap.Refresh();
        }

        public void DrawEllipse(Pen pen, int x, int y, int width, int height)
        {
            this.m_grahics.DrawEllipse(pen, x, y, width, height);
            this.m_picMap.Refresh();
        }

        public void DrawLine(Pen pen, Point[] pts)
        {
            this.m_grahics.DrawLines(pen, pts);
            this.m_picMap.Refresh();
        }

        public void DrawLine(Pen pen, Point pt1, Point pt2)
        {
            this.m_grahics.DrawLine(pen, pt1.X, pt1.Y, pt2.X, pt2.Y);
            this.m_picMap.Refresh();
        }

        public void DrawPoint(Pen pen, Brush brush, Rectangle rect)
        {
            this.m_grahics.FillEllipse(brush, rect);
            this.m_grahics.DrawEllipse(pen, rect);
            this.m_picMap.Refresh();
        }

        public void DrawPolygon(Pen pen, Brush brush, Point[] pts)
        {
            this.m_grahics.FillPolygon(brush, pts);
            this.m_grahics.DrawPolygon(pen, pts);
            this.m_picMap.Refresh();
        }

        public void DrawRectangle(Pen pen, Rectangle rect)
        {
            this.m_grahics.DrawRectangle(pen, rect);
            this.m_picMap.Refresh();
        }

        public void DrawString(string s, Font font, Brush brush, float x, float y)
        {
            this.m_grahics.DrawString(s, font, brush, x, y);
            this.m_picMap.Refresh();
        }
        #endregion
    }
}
